package com.hxzy.jsp.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import com.hxzy.jsp.connection.JNDIBBSConection;
import com.hxzy.jsp.entity.Post;

public class PostDAOImpl extends BaseDAO implements PostDAO {

	@Override
	public List<Post> findAll(int offset, int pageSize) {
		Connection conn = open();
		List<Post> postList = new LinkedList<>();
		try {
			PreparedStatement stmt = conn.prepareStatement("SELECT	* FROM post ORDER BY gmt_create DESC LIMIT ?,?");
			stmt.setInt(1, offset);
			stmt.setInt(2, pageSize);
			ResultSet rs = stmt.executeQuery();
			while(rs.next()) {
				Post post = new Post();
				post.setId(rs.getLong("id"));
				post.setTitle(rs.getString("title"));
				post.setContent(rs.getString("content"));
				post.setKeywords(rs.getString("keywords"));
				post.setUserId(rs.getLong("user_id"));
				post.setUserNickname(rs.getString("user_nickname"));
				post.setHits(rs.getLong("hits"));
				post.setLike(rs.getLong("like"));
				post.setIp(rs.getString("ip"));
				post.setGmtCreate(rs.getLong("gmt_create"));
				post.setGmtModified(rs.getLong("gmt_modified"));

				postList.add(post);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			return postList;
		}
		return postList;
	}

	@Override
	public Post find(long id) {
		Connection conn = open();
		Post post = null;
		try {
			PreparedStatement stmt = conn.prepareStatement("SELECT * FROM post where id = ? LIMIT 1");
			stmt.setLong(1, id);
			ResultSet rs = stmt.executeQuery();
			while(rs.next()) {
				post = new Post();
				post.setId(rs.getLong("id"));
				post.setTitle(rs.getString("title"));
				post.setContent(rs.getString("content"));
				post.setKeywords(rs.getString("keywords"));
				post.setUserId(rs.getLong("user_id"));
				post.setUserNickname(rs.getString("user_nickname"));
				post.setHits(rs.getLong("hits"));
				post.setLike(rs.getLong("like"));
				post.setIp(rs.getString("ip"));
				post.setGmtCreate(rs.getLong("gmt_create"));
				post.setGmtModified(rs.getLong("gmt_modified"));
				
				return post;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			return post;
		}
		return post;
	}

	@Override
	public List<Post> findUserLatestPosts(long userId, int limit) {
		Connection conn = open();
		List<Post> postList = new LinkedList<>();
		try {
			PreparedStatement stmt = conn.prepareStatement("SELECT * FROM post WHERE user_id = ? ORDER BY gmt_create DESC LIMIT ?");
			stmt.setLong(1, userId);
			stmt.setInt(2, limit);
			ResultSet rs = stmt.executeQuery();
			while(rs.next()) {
				Post post = new Post();
				post.setId(rs.getLong("id"));
				post.setTitle(rs.getString("title"));
				post.setContent(rs.getString("content"));
				post.setKeywords(rs.getString("keywords"));
				post.setUserId(rs.getLong("user_id"));
				post.setUserNickname(rs.getString("user_nickname"));
				post.setHits(rs.getLong("hits"));
				post.setLike(rs.getLong("like"));
				post.setIp(rs.getString("ip"));
				post.setGmtCreate(rs.getLong("gmt_create"));
				post.setGmtModified(rs.getLong("gmt_modified"));

				postList.add(post);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			return postList;
		}
		return postList;
	}

	@Override
	public int count() {
		// TODO Auto-generated method stub
		Connection conn = open();
		try {
			PreparedStatement stmt = conn.prepareStatement("SELECT COUNT(1) as post_count FROM post");
			ResultSet rs = stmt.executeQuery();
			while (rs.next()) {
				return rs.getInt("post_count");
			}
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
		return 0;
	}

	@Override
	public List<Post> findMy(String nickname) {
		// TODO Auto-generated method stub
		return null;
	}

	
	@Override
	public long hit(long hit) {
		// TODO Auto-generated method stub
		return 0;
	}

}
